<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: ASUS
  Date: 2023/5/13
  Time: 10:29
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<head>
    <link rel="icon" href="<c:url value="/static/images/favicon.ico"/>">
    <link rel="stylesheet" href="<c:url value="/static/lib/layui-v2.5.5/css/layui.css"/>" media="all">
    <link rel="stylesheet" href="<c:url value="/static/lib/font-awesome-4.7.0/css/font-awesome.min.css"/>" media="all">
    <link rel="stylesheet" href="<c:url value="/static/css/layuimini.css?v=2.0.1"/>" media="all">
    <link rel="stylesheet" href="<c:url value="/static/css/themes/default.css"/>" media="all">
    <link rel="stylesheet" href="<c:url value="/static/css/public.css"/>" media="all">
    <title>用户文章信息维护</title>
</head>
<div class="layuimini-container layuimini-page-anim">
    <div class="layui-form layuimini-form" lay-filter="userInfo-filter">
        <div class="layui-form-item">
            <label class="layui-form-label">用户ID</label>
            <div class="layui-input-block">
                <input type="text" name="userId" class="layui-input" disabled="true">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="nickName" class="layui-input" disabled="true">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
                <input type="email" name="email"  class="layui-input" disabled="true">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">余额</label>
            <div class="layui-input-block">
                <input type="number" name="money" value="" class="layui-input" disabled="true">
            </div>
        </div>
    </div>
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">文章名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="articleName4search" id="articleName4search"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <%--                        <div class="layui-inline">--%>
                        <%--                            <label class="layui-form-label">手机号</label>--%>
                        <%--                            <div class="layui-input-inline">--%>
                        <%--                                <input type="text" name="phone" autocomplete="off" class="layui-input">--%>
                        <%--                            </div>--%>
                        <%--                        </div>--%>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="addArticle"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="deleteArticle"> 删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="articleTable" lay-filter="articleTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="editArticle">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="deleteArticle">删除</a>
        </script>

    </div>
</div>
<script src="<c:url value="/static/lib/layui-v2.5.5/layui.js"/>" charset="utf-8"></script>
<script src="<c:url value="/static/js/lay-config.js?v=2.0.0"/>" charset="utf-8"></script>
<script>
    var user;
    layui.use(['form', 'table','miniPage','element'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage;

        user=JSON.parse('${userEdit}');

        form.val('userInfo-filter', {
            "userId":user.userId,
            "nickName": user.nickName,
            "money":user.money,
            "email":user.email
        });
        // /**
        //  * 初始化表单，要加上，不然刷新部分组件可能会不加载
        //  */
        // table.render();

        table.render({
            elem: '#articleTable',
            url: '/article/getArticleList',
            method:'post',
            where:{
                authorId: user.userId
            },
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 30},
                {field: 'aid',align:'center', title: 'ID',sort: true},
                {field: 'aname', title: '文章名'},
                {field: 'adesc', title: '文章描述简介'},
                {field: 'abody', title: '文章内容'},
                {title: '操作', minWidth: 130, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [5, 10, 15],
            limit: 5,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var table=layui.table;
            var result = JSON.stringify(data.field);
            console.log(result);
            var search=$("#articleName4search").val();
            console.log(search);
            //执行搜索重载
            table.reload('articleTable', {
                url: '/article/getArticleList',
                method:'post',
                where: {
                    authorId: user.userId,
                    search: search
                },
                page: {
                    curr: 1
                },
            });
            return false;
        });

        /**
         * toolbar事件监听
         */
        table.on('toolbar(articleTableFilter)', function (obj) {
            if (obj.event === 'addArticle') {   // 监听添加操作
                var contentAdd = "/article/add"
                var index = layer.open({
                    title: '添加文章',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['500px', '300px'],
                    content: contentAdd,
                    end: function (){
                        table.reload(
                            'articleTable', {
                                where: {
                                    authorId: user.userId
                                }
                            })
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'deleteArticle') {
                var checkStatus = table.checkStatus('articleTable')
                    , data = checkStatus.data;
                var aDel_idList = [];
                data.map(function(item){
                    aDel_idList.push({
                        articleId: item.aid,
                        ask: "1"
                    });
                });
                layer.confirm('真的删除这些文章吗:\n'+JSON.stringify(aDel_idList), function (index){
                    //执行 Ajax 后重载
                    $.ajax({
                        url: '/article/articleBatchDel',
                        method:'post',
                        data: JSON.stringify(aDel_idList),
                        dataType: "JSON",
                        contentType : 'application/json;charset=utf-8',
                        success:function(res){
                            if(res.status===200){
                                layer.msg(res.message,{icon:1,time:1000});
                                layer.close(index);
                                table.reload('articleTable',{
                                    where:{
                                        authorId: user.userId
                                    }
                                })
                            }else{
                                layer.msg(res.message,{icon:0,time:1000});
                                layer.close(index);
                            }
                        },
                        error:function(e){
                            console.log(e,e.message);
                            alert('error');
                        }
                    });
                });
            }
        });

        //监听表格复选框选择
        table.on('checkbox(articleTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(articleTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'editArticle') {
                var contentEdit = "/article/edit?articleId="+data.aid;
                var index = layer.open({
                    title: '编辑文章',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['500px', '300px'],
                    content: contentEdit,
                    end: function (){
                        table.reload('articleTable', {where: {
                            authorId: user.userId
                        }
                    })
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'deleteArticle') {
                var contentDel = "/article/deleteArticle"
                layer.confirm('真的删除该行么', function (index) {
                    $.ajax({
                        url: contentDel,
                        data:JSON.stringify({
                            articleId: data.aid,
                            ask: "1"
                        }),
                        contentType: "application/json;charset=UTF-8",
                        dataType: "json",
                        type: "POST",
                        success:function(res){
                            if(res.status===200){
                                layer.msg(res.message,{icon:1,time:1000});
                                layer.close(index);
                                table.reload('articleTable',{
                                    where:{
                                        authorId: user.userId
                                    }
                                })
                            }else{
                                layer.msg(res.message,{icon:0,time:1000});
                                layer.close(index);
                            }
                        },
                        error:function(e){
                            console.log(e,e.message);
                            alert('error');
                        }
                    });
                });
            }
        });
    });
</script>
